/*
 * Purchase.java
 *
 * Created on September 27, 2008, 2:38 AM
 */

package rrs;

/**
 *
 * @author Paul Molenda
 */
public class Purchase extends javax.swing.JFrame 
{
    /** Creates new form Purchase */
    public Purchase() 
    {
        initComponents();
        manager = new TransactionManager();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BtnCancelPurchase = new javax.swing.JButton();
        AddPanel = new javax.swing.JPanel();
        BtnAddToPur = new javax.swing.JButton();
        ComBoxPurQty = new javax.swing.JComboBox();
        lblProductCode = new javax.swing.JLabel();
        txtproductCode = new javax.swing.JTextField();
        lblSelectQty = new javax.swing.JLabel();
        TxtAreaPurchase = new java.awt.TextArea();
        BtnCompletePurchaseTransaction = new javax.swing.JButton();
        lblMessage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(rrs.RRSApp.class).getContext().getResourceMap(Purchase.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N

        BtnCancelPurchase.setText(resourceMap.getString("BtnCancelPurchase.text")); // NOI18N
        BtnCancelPurchase.setName("BtnCancelPurchase"); // NOI18N
        BtnCancelPurchase.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BtnCancelPurchaseMouseClicked(evt);
            }
        });

        AddPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        AddPanel.setName("AddPanel"); // NOI18N

        BtnAddToPur.setText(resourceMap.getString("BtnAddToPur.text")); // NOI18N
        BtnAddToPur.setName("BtnAddToPur"); // NOI18N
        BtnAddToPur.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BtnAddToPurMouseClicked(evt);
            }
        });

        ComBoxPurQty.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }));
        ComBoxPurQty.setName("ComBoxPurQty"); // NOI18N

        lblProductCode.setText(resourceMap.getString("lblProductCode.text")); // NOI18N
        lblProductCode.setName("lblProductCode"); // NOI18N

        txtproductCode.setText(resourceMap.getString("txtProdCode.text")); // NOI18N
        txtproductCode.setName("txtProdCode"); // NOI18N

        lblSelectQty.setText(resourceMap.getString("lblSelectQty.text")); // NOI18N
        lblSelectQty.setName("lblSelectQty"); // NOI18N

        javax.swing.GroupLayout AddPanelLayout = new javax.swing.GroupLayout(AddPanel);
        AddPanel.setLayout(AddPanelLayout);
        AddPanelLayout.setHorizontalGroup(
            AddPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AddPanelLayout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(AddPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtproductCode, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BtnAddToPur, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ComBoxPurQty, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblProductCode, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSelectQty))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        AddPanelLayout.setVerticalGroup(
            AddPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AddPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblProductCode)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtproductCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addComponent(lblSelectQty)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ComBoxPurQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(BtnAddToPur, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        TxtAreaPurchase.setBackground(resourceMap.getColor("TxtAreaPurchase.background")); // NOI18N
        TxtAreaPurchase.setEditable(false);
        TxtAreaPurchase.setName("TxtAreaPurchase"); // NOI18N

        BtnCompletePurchaseTransaction.setText(resourceMap.getString("BtnCompletePurchaseTransaction.text")); // NOI18N
        BtnCompletePurchaseTransaction.setName("BtnCompletePurchaseTransaction"); // NOI18N
        BtnCompletePurchaseTransaction.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BtnCompletePurchaseTransactionMouseClicked(evt);
            }
        });

        lblMessage.setFont(resourceMap.getFont("lblMessage.font")); // NOI18N
        lblMessage.setText(resourceMap.getString("lblMessage.text")); // NOI18N
        lblMessage.setName("lblMessage"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(BtnCancelPurchase))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(BtnCompletePurchaseTransaction))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMessage)
                            .addComponent(AddPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(TxtAreaPurchase, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(AddPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BtnCompletePurchaseTransaction, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(BtnCancelPurchase, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48))
                    .addComponent(TxtAreaPurchase, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(433, Short.MAX_VALUE)
                .addComponent(lblMessage)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void BtnCancelPurchaseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BtnCancelPurchaseMouseClicked
    if (BtnCancelPurchase.isEnabled() == true)
    {
    BtnAddToPur.setEnabled(false);
    BtnCompletePurchaseTransaction.setEnabled(false);
    BtnCancelPurchase.setEnabled(false);
    manager=null;
    lblMessage.setText("The order has been canceled!");       
    }
}//GEN-LAST:event_BtnCancelPurchaseMouseClicked

 /*
 User just clicked completeTransactionButton
 This method will send the information to the TransactionManager
 and if true it will begin the printing of receipt
 In this method, the balance will be printed
 */
private void BtnCompletePurchaseTransactionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BtnCompletePurchaseTransactionMouseClicked
    if (BtnCompletePurchaseTransaction.isEnabled() == true)
    {
    if (manager.completeTran(manager.receiptId))
         { 
            TxtAreaPurchase.selectAll();
            TxtAreaPurchase.setText("");
            TxtAreaPurchase.append(manager.printReceipt());
            manager=null;    
            BtnAddToPur.setEnabled(false);
            BtnCompletePurchaseTransaction.setEnabled(false);
            BtnCancelPurchase.setEnabled(false);
            lblMessage.setText("Transaction Complete!");    
        } 
    }
}//GEN-LAST:event_BtnCompletePurchaseTransactionMouseClicked

/*
 Button Ok was clicked.
*/
private void BtnAddToPurMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BtnAddToPurMouseClicked
if (BtnAddToPur.isEnabled() == true)
{
try
     {
     String productCode =  txtproductCode.getText();
     int qty = Integer.parseInt(ComBoxPurQty.getSelectedItem().toString());     
     //Here we will add the purchase to the Transaction manager
     //This sets up the transaction if true
        if (manager.addPurchase(productCode, qty))
           {
               TxtAreaPurchase.selectAll();
               TxtAreaPurchase.setText("");
               TxtAreaPurchase.append(manager.viewScreenMsg);
               lblMessage.setText("Product added to transaction.");
           }
        else
           {
                 lblMessage.setText("Unable to add product to transaction.");
           }
      }
catch(NumberFormatException e)
      {
            lblMessage.setText("Enter the correct information");
      }   
}
}//GEN-LAST:event_BtnAddToPurMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel AddPanel;
    private javax.swing.JButton BtnAddToPur;
    private javax.swing.JButton BtnCancelPurchase;
    private javax.swing.JButton BtnCompletePurchaseTransaction;
    private javax.swing.JComboBox ComBoxPurQty;
    private java.awt.TextArea TxtAreaPurchase;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblProductCode;
    private javax.swing.JLabel lblSelectQty;
    private javax.swing.JTextField txtproductCode;
    // End of variables declaration//GEN-END:variables
    private TransactionManager manager;     
}
